﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Silver3D.Graphics
{
    public class RenderTarget : Canvas
    {
        public SoftwareRenderer Renderer { get; private set; }
        private RenderContext renderContext;

        public RenderTarget()
            : base()
        {
            Renderer = new SoftwareRenderer();
            renderContext = new RenderContext();
            Renderer.RC = renderContext;

            SizeChanged += new SizeChangedEventHandler(Resize);
            CompositionTarget.Rendering += new EventHandler(Redraw);
        }

        private void Resize(object sender, SizeChangedEventArgs args)
        {
            renderContext.Resize((int)ActualWidth, (int)ActualHeight);
            this.Background = new ImageBrush() { ImageSource = renderContext.FrameBuffer };
        }

        private void Redraw(object sender, EventArgs args)
        {
            Renderer.Render();
        }
    }
}
